<template>
    <div class="content">
        <SearchTitle @searchClick="SearchTitleClick" />
        <div class="CenterBox">
            <div class="fhanBox" @click="fanhuifun">&lt;返回</div>
            <el-card>
                <div class="jsflexBox">
                    <div class="jsflexLabel">商品类型</div>
                    <div class="jsRightFlex">
                        <div :class="{ active: parameterData.type === item.dictValue }"
                            v-for="item in sysProductTypeData" :key="item.id" @click="splxTasbsfun(item.dictValue)">
                            {{ item.dictLabel }}
                        </div>
                    </div>
                </div>
                <div class="jsflexBox">
                    <div class="jsflexLabel">县区区域</div>
                    <div class="jsRightFlex">
                        <div :class="{ active: parameterData.regionId === item.dictValue }"
                            v-for="item in sysRegionData" :key="item.id" @click="xqTasbsfun(item.dictValue)">
                            {{ item.dictLabel }}
                        </div>
                    </div>
                </div>
            </el-card>
            <el-card style="margin-top: 20px;">
                <div style="color: #7C7C7C;">共找到{{ totalData }}件商品</div>
                <div class="spListBox">
                    <div class="spForBox" v-for="item in queryProductListData" :key="item.id"
                        @click="spxqdzfun(item.id)">
                        <img class="spForImgBox"
                            :src="item.logo"
                            alt="" loading="lazy">
                        <div class="spForTitle">{{ item.name }}</div>
                        <div class="spFormsName">{{ item.enterprise_name }}</div>
                        <div class="spForjgrs">
                            <div class="spForjgFlex">
                                <span style="font-size: 14px;">￥</span>
                                <span style="font-size: 20px;">{{ item.price }}</span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="fenyeBox">
                    <el-pagination v-model:current-page="parameterData.page" v-model:page-size="parameterData.limit"
                        :page-sizes="[100, 200, 300, 400]" background layout="prev, pager, next, jumper"
                        :total="totalData" @current-change="handleCurrentChange" />
                </div>
            </el-card>
        </div>
    </div>
</template>
<script setup>
import SearchTitle from '@/components/general/SearchTitle.vue';
import { queryProductList } from '@/api/api.js';
import { sysProductType, sysRegion } from '@/api/dictionary.js';
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
const router = useRouter();
const route = useRoute();
import { useStore } from '@/store/store';
const store = useStore()
onMounted(() => {
    // 顶部搜索跳转进来的
    if (route.query.searchType) {
        parameterData.value.name = route.query.name;
    }
    parameterData.value.type = route.query.type || '';
    sysProductTypefun()
    sysRegionfun()
    queryProductListfun()
})
const parameterData = ref({
    page: 1,
    limit: 20,
    type: '',
    regionId: '',
})
const totalData = ref(0);
const queryProductListData = ref([]);
const queryProductListfun = async () => {
    const res = await queryProductList(parameterData.value);
    totalData.value = res.count
    queryProductListData.value = res.data.map(item => {
        let url = item.upload.find(a => a.attachment_type === 'Cover')?.file_path || ''
        return {
            ...item,
            logo: url == 'UpLoadFiles\\shop.png' ? `https://image.pollinations.ai/prompt/${item.name.split('、')[0]}` : store.imgfengMian + url

        };
    })
}
// 顶部搜索点击事件
const SearchTitleClick = (name) => {
    parameterData.value.name = name;
    parameterData.value.page = 1;
    queryProductListfun()
}
const sysProductTypeData = ref([]);
const sysProductTypefun = async () => {
    const res = await sysProductType();
    sysProductTypeData.value = [
        {
            dictLabel: '全部',
            dictValue: '',
        },
        ...res.data
    ]
}
const sysRegionData = ref([]);
const sysRegionfun = async () => {
    const res = await sysRegion();
    sysRegionData.value = [
        {
            dictLabel: '全部',
            dictValue: '',
        },
        ...res.data
    ]
}
const splxTasbsfun = (val) => {
    parameterData.value.type = val;
    parameterData.value.page = 1
    queryProductListfun()
}
const xqTasbsfun = (val) => {
    parameterData.value.regionId = val;
    parameterData.value.page = 1
    queryProductListfun()
}
const handleCurrentChange = (val) => {
    parameterData.value.page = val;
    queryProductListfun()
}
const spxqdzfun = (id) => {
    router.push({
        path: '/ProductDetails',
        query: { id: id }
    });
}
const fanhuifun = () => {
    router.go(-1);
};
</script>
<style scoped lang="scss">
.content {
    width: 100%;
    height: 100%;
    padding-bottom: 60px;
    min-height: calc(100vh - 60px);
    background-size: 100%;
    background-color: #f3f9fe;
}

.fhanBox {
    font-size: 16px;
    color: #767879;
    margin: 10px 0;
    cursor: pointer;

    &:hover {
        color: $green-color;
    }
}

.spListBox {
    margin-top: 10px;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;

    .spForBox {
        width: calc(20% - 8px); // 5列布局（100% / 5 = 20%）
        flex-shrink: 0;
        padding: 10px;
        background: #fff;
        border-radius: 12px;
        cursor: pointer;

        .spForImgBox {
            width: 100%;
            height: 200px;
        }

        .spForTitle {
            font-size: 16px;
            color: #333333;
            margin-bottom: 5px;
            font-weight: bold;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
            max-width: 100%;
        }

        .spFormsName {
            font-size: 14px;
            color: #333333;
            margin-bottom: 5px;
            white-space: nowrap; // 禁止换行
            overflow: hidden; // 隐藏溢出内容
            text-overflow: ellipsis; // 显示省略号
        }

        .spForjgrs {
            display: flex;
            align-items: flex-end;

            .spForjgFlex {
                display: flex;
                align-items: flex-end;
                color: #EF1F1F;
                font-weight: bold;
            }
        }
    }
}

.jsflexBox {
    display: flex;
    margin-bottom: 20px;

    .jsflexLabel {
        width: 8%;
        font-size: 14px;
        color: #000000;
    }

    .jsRightFlex {
        display: flex;
        gap: 15px;
        font-weight: 300;
        font-size: 14px;
        color: #000000;
        cursor: pointer;
    }
}

.fenyeBox {
    margin-top: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-top: 1px solid #F1F1F1;
    padding-top: 20px;
}

.active {
    color: #2972FA;
    font-weight: bold;
}
</style>